threads.start(function () {
    var beginBtn;
    if (beginBtn = classNameContains("Button").textContains("立即开始").findOne(2000)) {
        beginBtn.click();
    }
});
sleep(1000);
if (!requestScreenCapture(true)) {
    toastLog("请求截图失败");
    exit();
}
threads.shutDownAll();//停止所有通过threads.start()启动的子线程
sleep(1000);
let count =0;
let temp, p, img, screenshot, img1, img2, name
startgame();

while(true){
    saveimage();sleep(500);
    testvictory();sleep(500);
    changemode();sleep(500);
    oiltest();sleep(500);
    //sleep(5*1000);
    //avoidstop();sleep(500);
}


function startgame(){
    temp = images.load("http://1.116.230.176:8001/pic/简单.png");
    // 截图并找图
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.6
    });
    if(p){
        console.log("找到了简单 " + p);
        click(p.x,p.y);
    }
    else{
        toast("请在指定页面！");
    }
    temp.recycle();sleep(1000);
    
    temp = images.load("http://1.116.230.176:8001/pic/立即前往.png");
    // 截图并找图
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.6
    });
    if(p){
        console.log("找到了立即前往 " + p);
        click(p.x+20,p.y+20);
    }
    else{
        toast("卡b了，555，联系我");
    }
    temp.recycle();sleep(2000);

    temp = images.load("http://1.116.230.176:8001/pic/知道了.png");
    // 截图并找图
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.9
    });
    if(p){
        console.log("知道了" + p);
        click(p.x+20,p.y+20);
    }
    temp.recycle();sleep(2000);
    
    temp = images.load("http://1.116.230.176:8001/pic/自律寻敌.png");
    // 截图并找图
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.9
    });
    if(p){
        console.log("没打开自律寻敌，打开中 " + p);
        click(p.x+20,p.y+20);
    }
    else{
        console.log("已打开自律寻敌");
    }
    temp.recycle();sleep(2000);

    temp = images.load("http://1.116.230.176:8001/pic/呼叫潜艇.png");
    // 截图并找图
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.9
    });
    if(p){
        console.log("没打开呼叫潜艇，打开中 " + p);
        click(p.x+20,p.y+20);
    }
    else{
        console.log("已打开呼叫潜艇");
    }
    temp.recycle();sleep(1000);

    temp = images.load("http://1.116.230.176:8001/pic/出击.png");
    // 截图并找图
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.6
    });
    if(p){
        console.log("找到出击了 " + p);
        click(p.x+20,p.y+20);
    }
    else{
        console.log("卡b了，555，联系我");
    }
    temp.recycle();sleep(1000);

}

function testvictory(){
    temp = images.load("http://1.116.230.176:8001/pic/点击继续.png");
    // 截图并找图
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.6
    });
    temp.recycle();
    if(p){
        console.log("找到点击继续 " + p);
        click(p.x+20,p.y+20);sleep(2000);
        while(true){
            click(1600,850);sleep(2000);
            temp = images.load("http://1.116.230.176:8001/pic/再次挑战.png");
            // 截图并找图
            p = findImage(images.captureScreen(), temp, {
                threshold: 0.6
            });
            temp.recycle();
            if(p){
                sleep(1000);
                console.log("找到再次挑战了 " + p);
                click(p.x+30,p.y+30);sleep(2000);
                break;
            }
		}
		temp = images.load("http://1.116.230.176:8001/pic/出击.png");
		// 截图并找图
		p = findImage(images.captureScreen(), temp, {
			threshold: 0.6
		});
		if(p){
			console.log("找到出击了 " + p);
			click(p.x+20,p.y+20);
		}
		else{
			console.log("卡b了，555，联系我");
		}
		temp.recycle();sleep(1000);
        

    }
}

function changemode(){
    temp = images.load("http://1.116.230.176:8001/pic/不消耗.png");
    // 截图并找图
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.6
    });
    temp.recycle();sleep(1000);
    if(p){
        count = count + 1;
        console.log("找到不消耗了 " + p);
        img = images.captureScreen();
        point = findColor(img, "#c0534e", {
            threshold: 4
        });
        
        if(point){
            click(point.x+5,point.y+5);sleep(1000);
        }
        else{
            console.log("寄了");
        }

        temp = images.load("http://1.116.230.176:8001/pic/返回.png");
        // 截图并找图
        p = findImage(images.captureScreen(), temp, {
            threshold: 0.6
        });
        if(p){
            console.log("找到返回了 " + p);
            click(p.x+40,p.y+40);
        }
        temp.recycle();sleep(2000);
        click(250,800);sleep(1000);

        switch(count){
            case 1:
                temp = images.load("http://1.116.230.176:8001/pic/普通.png");
                // 截图并找图
                p = findImage(images.captureScreen(), temp, {
                    threshold: 0.6
                });
                if(p){
                    console.log("找到普通了 " + p);
                    click(p.x+20,p.y+20);
                }
                temp.recycle();sleep(2000);
    
                
                break;
            case 2:
                temp = images.load("http://1.116.230.176:8001/pic/困难.png");
                // 截图并找图
                p = findImage(images.captureScreen(), temp, {
                    threshold: 0.6
                });
                if(p){
                    console.log("找到普通了 " + p);
                    click(p.x+20,p.y+20);
                }
                temp.recycle();sleep(2000);
                break;
            case 3:
                console.log("全部运行完毕");
                closeapp("碧蓝航线");
                engines.stopAll();
                break;  
            default:
                console.log("卡bug了,这他妈不可能");  
        }

        temp = images.load("http://1.116.230.176:8001/pic/立即前往.png");
        // 截图并找图
        p = findImage(images.captureScreen(), temp, {
            threshold: 0.6
        });
        if(p){
            console.log("找到立即前往了 " + p);
            click(p.x+20,p.y+20);
        }
        temp.recycle();sleep(2000);
    
        temp = images.load("http://1.116.230.176:8001/pic/出击.png");
        // 截图并找图
        p = findImage(images.captureScreen(), temp, {
            threshold: 0.6
        });
        if(p){
            console.log("找到出击了 " + p);
            click(p.x+20,p.y+20);
        }
        temp.recycle();sleep(2000);

    }
}

function saveimage(){
    files.createWithDirs("/sdcard/碧蓝航线脚本/screenshot1.jpg");
    screenshot = images.captureScreen();
    images.save(screenshot, "/sdcard/碧蓝航线脚本/screenshot1.jpg", "jpg", 100);
}

//截图保存，10秒截图，对比，100%卡死，执行
function avoidstop(){
    files.createWithDirs("/sdcard/碧蓝航线脚本/screenshot2.jpg");
    img1 = images.read("/sdcard/碧蓝航线脚本/screenshot1.jpg");
    temp = images.captureScreen();
    images.save(temp, "/sdcard/碧蓝航线脚本/screenshot2.jpg", "jpg", 100);
    img2 = images.read("/sdcard/碧蓝航线脚本/screenshot2.jpg");
    p = images.getSimilarity(img1, temp);
    console.log(p);
    img1.recycle();
    img2.recycle();
    if (p>2.7){
        toast("卡死了");
        //执行代码
        engines.execScriptFile("./bdocr.js");
        engines.myEngine().forceStop();
    }
}

function oiltest(){
    //石油检测，待写
    temp = images.load("http://1.116.230.176:8001/pic/没油了.png");
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.6
    });
    temp.recycle();
    if(p){
        stopscript = 1;
        console.log("没油了，停止运行: " + p);
        back();sleep(1000);
        closeapp("碧蓝航线");
        device.cancelKeepingAwake();
        //播放音乐
        media.playMusic("./music/运行完毕.mp3");
        //让音乐播放完
        sleep(media.getMusicDuration());sleep(1000);
        engines.stopAll();
    }
}

function moodtest(){
    //心情检测，代码待定,有bug,透
    temp = images.load("http://1.116.230.176:8001/pic/心情低.png");
    p = findImage(images.captureScreen(), temp, {
        threshold: 0.6
    });
    temp.recycle();
    if(p){
        stopscript = 1;
        console.log("心情太低，停止运行5分钟: " + p);
        back();
        device.cancelKeepingAwake();
        //播放音乐
        media.playMusic("./music/运行完毕.mp3");
        //让音乐播放完
        sleep(media.getMusicDuration());
        sleep(1000*60*5);
        temp = images.load("http://1.116.230.176:8001/pic/出击.png");
        // 截图并找图
        p = findImage(images.captureScreen(), temp, {
            threshold: 0.6
        });
        if(p){
            console.log("找到出击了 " + p);
            click(p.x+20,p.y+20);
        }
        temp.recycle();sleep(2000);
    }
}

function closeapp(packageName) {
    name = getPackageName(packageName); 
    if(!name){
        if(getAppName(packageName)){
            name = packageName;
        }else{
            return false;
        } 
    }
    app.openAppSetting(name);
    text(app.getAppName(name)).waitFor();  
    let is_sure = textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne();
    if (is_sure.enabled()) {
        textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne().click();
        sleep(1000);
        if (click("强行停止")){
            click("强行停止");
        }else{
            click("确定");
        }
        log(app.getAppName(name) + "应用已被关闭");
        sleep(1000);
        back();
    } else {
        log(app.getAppName(name) + "应用不能被正常关闭或不在后台运行");
        back();
    }
}